/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package practica1.util;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JTextField;

/**
 * Componente que extiende de JTextField y permite solo escribir números
 * @author Miguel González - Ceura
 */
public class JTextFieldNumerico extends JTextField {
    
    /**
     * Constructor por defecto
     */
    public JTextFieldNumerico() {
        addKeyListener(new KeyListener() {
            @Override
            public void keyTyped(KeyEvent e) {
                char c = e.getKeyChar();

                //Si no es un número y no es delete o backspace consumimos el evento
                if (!(Character.isDigit(c) ||
                    (c == KeyEvent.VK_BACK_SPACE) ||
                        (c == KeyEvent.VK_DELETE))) {
                    e.consume();
                }
            }

            @Override
            public void keyPressed(KeyEvent e) {}

            @Override
            public void keyReleased(KeyEvent e) {}
            
        });
    }
}
